package com.learn.worksession.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @program: loginproject
 * @description
 * @author: luffy
 * @create: 2020-08-13 00:33
 */
public class RequestInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器起作用");
        System.out.println(request.getRequestURI());
        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");
        if (username==null) {
            System.out.println(request.getRequestURI());
            response.sendRedirect(request.getContextPath()+"/login/toLogin");
            return false;
        }else {
            System.out.println(request.getRequestURI());
            return true;
        }
    }
}
